package com.frinder.base.global;

import lombok.Getter;

import static com.frinder.base.global.ResultGenerator.ResultCode.FAIL;
import static com.frinder.base.global.ResultGenerator.ResultCode.SUCCESS;

/**
 * @ClassName ResultGenerator
 * @Author frinder
 * @Description 响应结果生成工具
 * @Date 2019/1/19 16:21
 * @Version 1.0
 */
public class ResultGenerator {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_ERROR_MESSAGE = "ERROR";

    public enum ResultCode {
        SUCCESS(200),
        FAIL(400),
        /**
         * 调用服务失败
         */
        CALL_SERVICE_ERROR(40001);

        @Getter
        private int code;

        ResultCode(int code) {
            this.code = code;
        }

    }

    public static Result gen(ResultCode code, Object data, String message) {
        return Result.builder()
                .code(code.code)
                .data(data)
                .message(message)
                .build();
    }

    public static Result gen(ResultCode code, Object data) {
        return gen(code, data, "");
    }

    public static Result gen(ResultCode code, String message) {
        return gen(code, null, message);
    }

    public static Result genSuccessResult(Object data, String message) {
        return Result.builder()
                .code(SUCCESS.code)
                .data(data)
                .message(message)
                .build();
    }

    public static Result genSuccessResult() {
        return genSuccessResult(null, DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result genSuccessResult(Object data) {
        return genSuccessResult(data, DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result genSuccessResult(String message) {
        return genSuccessResult(null, message);
    }

    public static Result genFailResult(Object data, String message) {
        return Result.builder()
                .code(FAIL.code)
                .data(data)
                .message(message)
                .build();
    }

    public static Result genFailResult(String message) {
        return genFailResult(null, message);
    }

    public static Result genFailResult() {
        return genFailResult(null, DEFAULT_ERROR_MESSAGE);
    }
}
